package com.lange.autocode.sysManager;

import cn.net.autocode.core.utils.UserThreadLocalUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.IdWorker;
import cn.net.autocode.core.bean.ResultT;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/sysManager/sysMessage")
@RestController
public class SysMessage {
    private final Dao dao;

    public SysMessage(Dao dao) {
        this.dao = dao;
    }

    @RequestMapping("saveMsgSetingDept")
    @Transactional
    public ResultT saveMsgSetingDept(@RequestBody JSONObject param){
        String msgSettingId = param.getString("msgSettingId");
        JSONArray deptList = param.getJSONArray("deptList");
        List<String> list = deptList.toJavaList(String.class);
        if(list.isEmpty()){
            dao.executeUpdate("delete from sys_message_user where msg_setting_id=? and type='dept'",msgSettingId);
        }else{
            dao.executeUpdate("delete from sys_message_user where msg_setting_id=? and type='dept' and name not in (?)",msgSettingId,list);
            List<String> exists = dao.queryListValue("select name from sys_message_user where msg_setting_id=? and type='dept'",String.class,msgSettingId);
            list.removeAll(exists);
            for(String dept:list){
                dao.executeUpdate("insert into sys_message_user(id,type,name,label,msg_setting_id) values(?,?,?,?,?)", IdWorker.ulid(),"dept",dept,dept,msgSettingId);
            }
        }
        return ResultT.success();
    }

    @RequestMapping("saveMsgSetingUser")
    @Transactional
    public ResultT saveMsgSetingUser(@RequestBody JSONObject param){
        String msgSettingId = param.getString("msgSettingId");
        JSONArray userList = param.getJSONArray("userList");
        List<String> list = userList.toJavaList(String.class);
        if(list.isEmpty()){
            dao.executeUpdate("delete from sys_message_user where msg_setting_id=? and type='user'",msgSettingId);
        }else{
            dao.executeUpdate("delete from sys_message_user where msg_setting_id=? and type='user' and name not in (?)",msgSettingId,list);
            List<String> exists = dao.queryListValue("select name from sys_message_user where msg_setting_id=? and type='user'",String.class,msgSettingId);
            list.removeAll(exists);
            if(!list.isEmpty()){
                Map<String, Object> map = dao.queryMapVT("select username value,fullname text from sys_users where username in (?)",list);
                for(String username:list){
                    dao.executeUpdate("insert into sys_message_user(id,type,name,label,msg_setting_id) values(?,?,?,?,?)", IdWorker.ulid(),"user",username,map.get(username),msgSettingId);
                }
            }

        }
        return ResultT.success();
    }

    @RequestMapping("fabu")
    @Transactional
    public ResultT fabu(@RequestBody JSONObject param){
        String id = param.getString("id");
        String status = dao.queryValue("select status from sys_message_setting where id=?",String.class,id);
        if("Draft".equals(status)){
            dao.executeUpdate("update sys_message_setting set status=?,release_time=?,release_user=? where id=?","Release",new Date(), UserThreadLocalUtils.getUserName(),id);
        }else{
            ResultT.fail("只能发布草稿状态的信息");
        }
        return ResultT.success();
    }


    @RequestMapping("cancelFabu")
    @Transactional
    public ResultT cancelFabu(@RequestBody JSONObject param){
        String id = param.getString("id");
        String status = dao.queryValue("select status from sys_message_setting where id=?",String.class,id);
        if("SendOut".equals(status)){
            ResultT.fail("信息已发送，不能取消！");
        }else{
            dao.executeUpdate("update sys_message_setting set status=? where id=?","Draft",id);
        }
        return ResultT.success();
    }
}
